<?php
/*
 * Controller creato per la gestione dei Post
 *
 *  Author: Davide Monfrecola
 *
 */
App::import('sanitize'); // vedere http://book.cakephp.org/view/1183/Data-Sanitization

class CurriculumsController extends AppController {

    var $helpers = array('Html', 'Form', 'Javascript', 'TinyMce', 'Session');
    var $name = 'Curriculums';

    var $uses = array('Curriculum', 'Person'); // specifico i modelli che utilizza il controller

    function isAuthorized() {
        return true;
    }
    
    function index() {
         $this->pageTitle = 'Curriculums list';
         
         // se richiamato da countbox ritorno solo il numero di record
         if (isset($this->params['requested'])) {
            return $this->Person->find('count');
         }
         else {
            
            $this->set('curriculums', $this->XmlFile->find('all')); // view variable
         }
         
     }
     
    function count() {
        if (isset($this->params['requested'])) {
            return $this->XmlFile->find('count');;
        }

     }
     
    function final_step() {
         
         $this->Session->setFlash('Procedura inserimento curriculum terminata con successo');
         $this->set('user_id', $this->Auth->user('id'));
     }

     function download_xml($person_id) {

        //Configure::write('debug', 0); // turn off debug output

        $person = $this->Person->findById($person_id);
        
        if($person['Person']['modified'] == '1')
            $this->set('xml', $this->create_xml($person['Person']['id']));
        else
            $this->set('xml', $person['Person']['xml']);
        $this->set('content_type', 'text/xml');
        $this->set('filename', $person['Person']['surname']);
        $this->render('download', 'file');
     
     }

     function create_xml($person_id) {
         
         $person = $this->Person->findById($person_id);
         $person = $this->Person->find('all', array('conditions' => "Person.id = {$person_id}", 'recursive' => '1'));

         /* **************************************
          * Informazioni personali
          * **************************************/
         $xml = <<<EOF
<europass:learnerinfo xmlns:europass="http://europass.cedefop.europa.eu/Europass/V2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://europass.cedefop.europa.eu/Europass/V2.0 http://europass.cedefop.europa.eu/xml/EuropassSchema_V2.0.xsd" locale="en_GB">
<docinfo>
    <issuedate>2008-10-10T11:50:08+03:00</issuedate>
    <xsdversion>V2.0</xsdversion>
    <comment>Automatically generated Europass CV</comment>
</docinfo>
<identification>
    <firstname>{$person[0]['Person']['name']}</firstname>
    <lastname>{$person[0]['Person']['surname']}</lastname>
    <contactinfo>
        <address>
            <addressLine>{$person[0]['Person']['address']}</addressLine>
            <municipality>{$person[0]['Town']['name']}</municipality>
            <postalCode>{$person[0]['Person']['postalCode']}</postalCode>
            <country>
                <code>IT</code>
                <label>Italia</label>
            </country>
        </address>
        <telephone>{$person[0]['Person']['telephone_number']}</telephone>
        <fax>{$person[0]['Person']['fax']}</fax>
        <mobile>{$person[0]['Person']['mobile']}</mobile>
        <email>{$person[0]['Person']['email']}</email>
    </contactinfo>
    <demographics>
        <birthdate>{$person[0]['Person']['birthdate']}</birthdate>
        <gender>{$person[0]['Person']['gender']}</gender>
        <nationality>
            <code></code>
            <label></label>
        </nationality>
    </demographics>
</identification>
<workexperiencelist>
EOF;
        /* **************************************
         * Esperienze di lavoro
         * **************************************/
        foreach($person[0]['WorkExperience'] as $workExperience) {

            $position = $this->Person->WorkExperience->Position->findById($workExperience['position_id']);

            $options = array('conditions' => "Company.id = {$workExperience['company_id']}", 'recursive' => '0');
            $company = $this->Person->WorkExperience->Company->find('all', $options);

            $from_date = explode('-',$workExperience['from']);
            $to_date = explode('-',$workExperience['to']);

            $xml .= "
                <workexperience>
            <period>
                <from>
                    <year>{$from_date[0]}</year>
                    <month>{$from_date[1]}</month>
                    <day>{$from_date[2]}</day>
                </from>
                <to>
                    <year>{$to_date[0]}</year>
                    <month>{$to_date[1]}</month>
                    <day>{$to_date[2]}</day>
                </to>
            </period>
            <position>
                <code>{$position['Position']['code']}</code>
                <label>{$position['Position']['name']}</label>
            </position>
            <activities>{$workExperience['activity']}</activities>
            <employer>
                <name>{$company[0]['Company']['name']}</name>
                <address>
                    <addressLine>{$company[0]['Company']['address']}</addressLine>
                    <municipality>{$company[0]['Town']['name']}</municipality>
                    <postalCode>{$company[0]['Company']['postalCode']}</postalCode>
                    <country>
                        <code></code>
                        <label></label>
                    </country>
                </address>
                <sector>
                    <code>{$company[0]['Sector']['id']}</code>
                    <label>{$company[0]['Sector']['name']}</label>
                </sector>
            </employer>
            </workexperience>";       


        }
        $xml .= "
            </workexperiencelist>
            <educationlist>";
        /* **************************************
         * Esperienze di lavoro
         * **************************************/
        foreach($person[0]['EducationExperience'] as $educationExperience) {

            $position = $this->Person->EducationExperience->Position->findById($workExperience['position_id']);
            $title = $this->Person->EducationExperience->Title->findById($educationExperience['title_id']);

            $options = array('conditions' => "Organization.id = {$educationExperience['organization_id']}", 'recursive' => '0');
            $organization = $this->Person->EducationExperience->Organization->find('all', $options);
            $from_date = explode('-',$educationExperience['from']);
            $to_date = explode('-',$educationExperience['to']);

            $xml .= "
            <education>
                  <period>
                    <from>
                        <year>{$from_date[0]}</year>
                        <month>{$from_date[1]}</month>
                        <day>{$from_date[2]}</day>
                    </from>
                    <to>
                        <year>{$to_date[0]}</year>
                        <month>{$to_date[1]}</month>
                        <day>{$to_date[2]}</day>
                    </to>
            </period>
            <title>{$title['Title']['name']}</title>
            <skills>{$educationExperience['skills']}</skills>         
                        
            <position>
                <code>{$position['Position']['code']}</code>
                <label>{$position['Position']['name']}</label>
            </position>
            <organisation>
                <name>{$organization[0]['Organization']['name']}</name>
                <address>
                    <addressLine>{$organization[0]['Organization']['address']}</addressLine>
                    <municipality>{$organization[0]['Town']['name']}</municipality>
                    <postalCode></postalCode>
                    <country>
                        <code></code>
                        <label></label>
                    </country>
                </address>
                <type>{$organization[0]['OrganizationType']['name']}</type>
            </organisation>
                ";       


        }
        $xml .= "
        </educationlist>
            <languagelist>";
        /* **************************************
         * Lingue
         * **************************************/
        foreach($person[0]['LanguageExperience'] as $experience) {

            $options = array('conditions' => "LanguageExperience.id = {$experience['id']}", 'recursive' => '0');
            $languageExperience = $this->Person->LanguageExperience->find('all', $options);
            
            if($languageExperience[0]['LanguageExperience']['mother_language'] == '1') {
                
                $xml .="
                    <language xsi:type=\"europass:mother\">
                        <code>{$languageExperience[0]['Language']['code']}</code>
                        <label>{$languageExperience[0]['Language']['name']}</label>
                    </language>";
                
            }
            else {
               
                $xml .="
                  <language xsi:type=\"europass:foreign\">
                      <code>{$languageExperience[0]['Language']['code']}</code>
                      <label>{$languageExperience[0]['Language']['name']}</label>
                      <level>
                            <listening>{$languageExperience[0]['ListeningLevel']['name']}</listening>
                            <reading>{$languageExperience[0]['ReadingLevel']['name']}</reading>
                            <spokeninteraction>{$languageExperience[0]['SpokenInteractionLevel']['name']}</spokeninteraction>
                            <spokenproduction>{$languageExperience[0]['SpokenProductionLevel']['name']}</spokenproduction>
                            <writing>{$languageExperience[0]['WritingLevel']['name']}</writing>
                      </level>
                  </language>";
                
            }
        }
             return $xml;
         }

}
?>
